package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.pogo.User;
import com.tanhua.server.pogo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TodayBestService {

//    @Autowired
//    private UserService userService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private UserInfoService userInfoService;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    public TodayBest queryTodayBest() {
        //校验token是否有效，通过sso接口进行校验
        User user= UserThreadLocal.get();
        //查询今日佳人
        TodayBest todayBest=this.recommendUserService.queryTodayBest(user.getId());
        if (null==todayBest){
            //未查询到，给一个默认推荐用户
             todayBest = new TodayBest();
             todayBest.setId(defaultUser);
             todayBest.setFateValue(80l);//固定缘分值
        }
        //补全个人信息
        UserInfo userInfo=this.userInfoService.queryUserInfoByUserId(todayBest.getId());
        if (null==userInfo){
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
        todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");
        todayBest.setAge(userInfo.getAge());

        return todayBest;
    }

    /**
     * 查询推荐列表
     * @param queryParam
     * @return
     */
    public PageResult queryRecommendation(RecommendUserQueryParam queryParam) {
        //校验token是否有效，通过sso接口进行校验
        User user=UserThreadLocal.get();
        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        PageInfo<RecommendUser> pageInfo = this.recommendUserService.queryRecommendUserList(user.getId(), queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)){
            //没有查到推荐的用户列表
            return pageResult;
        }
        //填充个人信息
        //收集推荐用户的id
        HashSet<Long> userIds = new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }

        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        //用户id参数
        queryWrapper.in("user_id",userIds);

        if (StringUtils.isNotEmpty(queryParam.getGender())){
            //用户选择了性别筛选，查询条件需要增加参数
//            queryWrapper.eq("sex",StringUtils.equals(queryParam.getGender(),"man")?1:2);
        }
        if (StringUtils.isNotEmpty(queryParam.getCity())){
            //用户选择了城市筛选
//            queryWrapper.like("city",queryParam.getCity());
        }
        if (null==queryParam.getAge()){
            //设置年龄参数，条件小于等于
//            queryWrapper.le("age",queryParam.getAge());
        }
        List<UserInfo> userInfoList=this.userInfoService.queryUserInfoList(queryWrapper);
            if (CollectionUtils.isEmpty(userInfoList)){
                //没有查询到用户的基本信息
                return pageResult;
            }

         List<TodayBest> todayBests=new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {

            TodayBest todayBest = new TodayBest();

            todayBest.setId(userInfo.getId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
            todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");
            todayBest.setAge(userInfo.getAge());

            for (RecommendUser record : records) {
                if (record.getUserId().longValue()==userInfo.getUserId().longValue()){
                    //设置缘分值
                    double score = Math.floor(record.getScore());//缘分值取整
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }

            todayBests.add(todayBest);
        }

        //按照缘分值进行倒叙排序
        Collections.sort(todayBests, new Comparator<TodayBest>() {
            @Override
            public int compare(TodayBest o1, TodayBest o2) {
                int i = new Long(o2.getFateValue() - o1.getFateValue()).intValue();
                return i;
            }
        });

        pageResult.setCounts(todayBests.size());
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
